package com.example.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class DetailListAdapter extends RecyclerView.Adapter<DetailListAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();
    //格式化时间
    private SimpleDateFormat mUpdateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mMinDurationFormat = new SimpleDateFormat("mm:ss");
    private SimpleDateFormat mHourDurationFormat = new SimpleDateFormat("HH:mm:ss");
    private ItemClickListener mItemClickListener = null;
    private String mTime;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_detail, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        //找到控件
        final View itemView = holder.itemView;
        //顺序ID
        TextView orderTv = itemView.findViewById(R.id.update_itemid_text);
        TextView titleTv = itemView.findViewById(R.id.update_title_text);
        TextView playCountTv = itemView.findViewById(R.id.update_playcount_text);
        TextView timeTv = itemView.findViewById(R.id.update_time_text);
        TextView dateTv = itemView.findViewById(R.id.update_date_text);
        //设置数据
        Track track = mDetailData.get(position);
        orderTv.setText(position + 1 + "");
        titleTv.setText(track.getTrackTitle() + "");
        playCountTv.setText(track.getPlayCount() + "");
        //声音时长是获得的单位是秒必须乘以1000再用格式转换，格式转换单位是毫秒
        //转换时区
        mHourDurationFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        mMinDurationFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        mUpdateDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        if (track.getDuration() > 60 * 60) {
            mTime = mHourDurationFormat.format(track.getDuration() * 1000);
        }else{
            mTime = mMinDurationFormat.format(track.getDuration() * 1000);
        }
        timeTv.setText(mTime + "");
        String date = mUpdateDateFormat.format(track.getUpdatedAt());
        dateTv.setText(date + "");

        //设置item的点击事件
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(v.getContext(), "you click +" + position + "item", Toast.LENGTH_SHORT).show();
                if (mItemClickListener != null) {
                    //参数要有列表和位置
                    mItemClickListener.onItemClick(mDetailData,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        mDetailData.clear();
        mDetailData.addAll(tracks);
        //更新数据
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //暴露点击事件给activity
    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(List<Track> detailData, int position);
    }
}
